package org.tyut.xyl.game;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.liugang.tugofwar.R;

import org.tyut.xyl.game.util.GsonUtil;
import org.tyut.xyl.game.util.Information;
import org.tyut.xyl.game.util.OkHttpUtil;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

public class MyHistoryActivity extends AppCompatActivity {
    private final static int UPDATE_HIS = 1;

    private String his1;
    private String his2;
    private String his3;
    private final String[] roomid = new String[3];
    private final String[] mycount = new String[3];
    private final String[] myresult = new String[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_history);

        FormBody formBody = new FormBody.Builder()
                .add("username", MainActivity.username)
                .build();
        Request request = new Request.Builder().url(Information.URL + "history").post(formBody).build();
        Call call = OkHttpUtil.getOkHttpClient().newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("ERROR", e.toString());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String string = response.body().string();
                List<Map<String, String>> maps = GsonUtil.getGsonBuilder().create().fromJson(string, List.class);
                Log.i("INF", maps.toString());
                for (int i = 0; i < 3; i++) {
                    if (maps.size() - 1 < i) {
                        break;
                    }
                    Map<String, String> stringStringMap = maps.get(i);
                    roomid[i] = stringStringMap.get("roomid");
                    mycount[i] = stringStringMap.get("mycount");
                    myresult[i] = stringStringMap.get("result");
                }
                Message message = new Message();
                message.what = UPDATE_HIS;
                handler.sendMessage(message);

            }
        });
//        HttpUtil.sendHttpRequest("history", MainActivity.username + ",", new HttpCallbackListener() {
//            @Override
//            public void onFinish(String response) {
//                String[] data = response.split("#");
//                String[] info = data[0].split(",");
//                meancount = Integer.parseInt(info[0]);
//                wintimes = Integer.parseInt(info[1]);
//                winradio = Integer.parseInt(info[2]);
//                String[] his1 = data[1].split(",");
//                String[] his2 = data[2].split(",");
//                String[] his3 = data[3].split(",");
//                roomid[0] = his1[0];
//                roomid[1] = his2[0];
//                roomid[2] = his3[0];
//                mycount[0] = his1[1];
//                mycount[1] = his2[1];
//                mycount[2] = his3[1];
//                myresult[0] = his1[2];
//                myresult[1] = his2[2];
//                myresult[2] = his3[2];
//                for (int i = 0; i < 3; i++) {
//                    if (roomid[i].equals("0")) roomid[i] = "";
//                    if (mycount[i].equals("0")) mycount[i] = "";
//                    if (myresult[i].equals("0")) myresult[i] = "";
//                    if (myresult[i].equals("win")) myresult[i] = "胜";
//                    if (myresult[i].equals("lose")) myresult[i] = "败";
//                }
//                Message message = new Message();
//                message.what = UPDATE_HIS;
//                handler.sendMessage(message);
//            }
//
//            @Override
//            public void onError(Exception e) {
//
//            }
//        });
        Button return_main = (Button) findViewById(R.id.his_returnmain);
        return_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyHistoryActivity.this, GameMainActivity.class);
                startActivity(intent);

            }
        });
    }

    private void updatehistory() {
        TextView mytitle = (TextView) findViewById(R.id.his_mytitle);


        TextView his_roomid1 = (TextView) findViewById(R.id.his_roomid1);
        TextView his_roomid2 = (TextView) findViewById(R.id.his_roomid2);
        TextView his_roomid3 = (TextView) findViewById(R.id.his_roomid3);
        TextView his_mycount1 = (TextView) findViewById(R.id.his_mycount1);
        TextView his_mycount2 = (TextView) findViewById(R.id.his_mycount2);
        TextView his_mycount3 = (TextView) findViewById(R.id.his_mycount3);
        TextView his_myresult1 = (TextView) findViewById(R.id.his_result1);
        TextView his_myresult2 = (TextView) findViewById(R.id.his_result2);
        TextView his_myresult3 = (TextView) findViewById(R.id.his_result3);

        his_roomid1.setText(roomid[0]);
        his_mycount1.setText(mycount[0]);
        his_myresult1.setText(myresult[0]);
        his_roomid2.setText(roomid[1]);
        his_mycount2.setText(mycount[1]);
        his_myresult2.setText(myresult[1]);
        his_roomid3.setText(roomid[2]);
        his_mycount3.setText(mycount[2]);
        his_myresult3.setText(myresult[2]);

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_HIS:
                    updatehistory();
                    break;
                default:
                    break;
            }
        }
    };
}
